package com.controller;

import com.util.gen.ImageGenUtil;
import com.util.pdf.PdfUtil;
import com.util.zip.MutithreadZipUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.IOException;

/**
 * @author Created by liangjiaming on 2020/12/29
 * @title
 * @Desc
 */
@RestController
@RequestMapping("/tool/v2")
@Slf4j
public class ToolControllerV2 {

    @Value("${file.srcImgsDirPath}")
    private String srcImgsDirPath;

    @Value("${file.srcRawsDirPath}")
    private String srcRawsDirPath;

    @Value("${file.dstDirPath}")
    private String dstDirPath;

    @GetMapping("/getImage")
    public String getImage() throws Exception {
        return "image.jpg";
    }

    @GetMapping("/getResizeImage")
    public String getResizeImage(@RequestParam Integer width, @RequestParam Integer height) throws Exception {
        String dstFilePath = getDstFilePath(".png");
        ImageGenUtil.genImage(width, height, dstFilePath);
        return correctPath(dstFilePath);
    }

    @GetMapping("/getZipFile")
    public String getZipFile() throws IOException {
        String dstFilePath = getDstFilePath(".zip");
        MutithreadZipUtil.createZip(srcRawsDirPath, dstFilePath);
        return correctPath(dstFilePath);
    }

    @GetMapping("/getImagePdf")
    public String getImagePdf() throws IOException {
        String dstFilePath = getDstFilePath(".pdf");
        PdfUtil.imagesToPdf(srcImgsDirPath, dstFilePath);
        return correctPath(dstFilePath);
    }

    private String getDstFilePath(String ext) {
        return dstDirPath + File.separator + System.currentTimeMillis() + ext;
    }

    private String correctPath(String dstFilePath) {
        return dstFilePath.replace(dstDirPath + File.separator, "");
    }

}
